package org.lipert.rccalc;

import org.lipert.rccalc.fragments.ChargingFragment;
import org.lipert.rccalc.fragments.CsFragment;
import org.lipert.rccalc.fragments.GearRatioFragment;
import org.lipert.rccalc.fragments.MaxSpeedFragment;
import org.lipert.rccalc.fragments.OilFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;

public class RCCalc extends SherlockFragmentActivity implements OnClickListener,
		TabListener {

	public static int THEME = R.style.Theme_Sherlock;

	private Tab csTab;
	private Tab maxSpeedTab;
	private Tab gearRatioTab;
	private Tab chargingTab;

	private Tab oilTab;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(RCCalc.THEME);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);

		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		csTab = getSupportActionBar().newTab();
		csTab.setText(R.string.csTabText);
		csTab.setTabListener(this);
		getSupportActionBar().addTab(csTab);

		maxSpeedTab = getSupportActionBar().newTab();
		maxSpeedTab.setText(R.string.maxSpeedTabText);
		maxSpeedTab.setTabListener(this);
		getSupportActionBar().addTab(maxSpeedTab);

		gearRatioTab = getSupportActionBar().newTab();
		gearRatioTab.setText(R.string.gearRatioTabText);
		gearRatioTab.setTabListener(this);
		getSupportActionBar().addTab(gearRatioTab);

		chargingTab = getSupportActionBar().newTab();
		chargingTab.setText(R.string.chargingTabText);
		chargingTab.setTabListener(this);
		getSupportActionBar().addTab(chargingTab);
		
		oilTab = getSupportActionBar().newTab();
		oilTab.setText(R.string.oilTabText);
		oilTab.setTabListener(this);
		getSupportActionBar().addTab(oilTab);
		
	
	}

	/*
	 * @Override public boolean onCreateOptionsMenu(Menu menu) {
	 * getSupportMenuInflater().inflate(R.menu.activity_main, menu); return
	 * true; }
	 */

	

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		if (tab == csTab) {
			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

			CsFragment fragment = new CsFragment();
			fragmentTransaction.replace(R.id.main_container, fragment);
			fragmentTransaction.commit();
		}
		if (tab == maxSpeedTab) {
			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

			MaxSpeedFragment fragment = new MaxSpeedFragment();
			fragmentTransaction.replace(R.id.main_container, fragment);
			fragmentTransaction.commit();
		}
		if (tab == gearRatioTab) {
			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

			GearRatioFragment fragment = new GearRatioFragment();
			fragmentTransaction.replace(R.id.main_container, fragment);
			fragmentTransaction.commit();
		}
		if (tab == chargingTab) {
			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

			ChargingFragment fragment = new ChargingFragment();
			fragmentTransaction.replace(R.id.main_container, fragment);
			fragmentTransaction.commit();
		}
		if (tab == oilTab) {
			FragmentManager fragmentManager = getSupportFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

			OilFragment fragment = new OilFragment();
			fragmentTransaction.replace(R.id.main_container, fragment);
			fragmentTransaction.commit();
		}

	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		/*
		 * SubMenu sub = menu.addSubMenu("Theme"); sub.add(0,
		 * R.style.Theme_Sherlock, 0, "Default"); sub.add(0,
		 * R.style.Theme_Sherlock_Light, 0, "Light"); sub.add(0,
		 * R.style.Theme_Sherlock_Light_DarkActionBar, 0,
		 * "Light (Dark Action Bar)");
		 * sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |
		 * MenuItem.SHOW_AS_ACTION_WITH_TEXT); return true;
		 */

		SubMenu sub = menu.addSubMenu(R.string.textSubMenu);
		sub.add(0, R.string.title_activity_about, 0, "O autorze");
		sub.getItem().setShowAsAction(
				MenuItem.SHOW_AS_ACTION_ALWAYS
						| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		
		/*SubMenu sub2 = menu.addSubMenu("Inne");
		sub2.add(0, R.string.title_activity_about, 0, "XXX");
		sub2.getItem().setShowAsAction(
				MenuItem.SHOW_AS_ACTION_ALWAYS
						| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
*/
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		/*if (item.getItemId() == android.R.id.home || item.getItemId() == 0) {
			return false;
		}
		THEME = item.getItemId();
		Toast.makeText(this, "Theme changed to \"" + item.getItemId() + "\"",
				Toast.LENGTH_SHORT).show();
*/
		
		
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

}
